package com.alex.controller;

import cn.hutool.http.HttpUtil;
import com.alex.entityVo.ArticleSearchVo;
import com.alex.service.ParseEntityService;
import com.alex.service.StaticService;
import com.alex.vo.PmUtAidInfoNsfcHosp;
import com.alex.vo.PmUtAidInfoNsfcHospMapper;
import com.alibaba.fastjson.JSONObject;

import common.beans.ResultBean;
import common.util.CheckUtil;
import common.util.StringUtil;
import common.util.UserUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RequestMapping("/his-fund-search")
@RestController
@Slf4j
public class HisFundController {

    @Resource
    ParseEntityService parseEntityService;

    // 未来学习中心的
    public static String RESTURL_ES = "http://10.1.1.11:29000";

    @Resource
    StaticService staticService;


    @Resource
    PmUtAidInfoNsfcHospMapper pmUtAidInfoNsfcHospMapper;

    @PostMapping(value = "search")
    public ResultBean<Object> search(@RequestBody ArticleSearchVo articleSearchVo) {
        log.info("articleSearchVo+" + articleSearchVo);
        Integer uid = UserUtil.getUserUid();
        Integer gid = UserUtil.getUserGid();
        String uname = UserUtil.getUserUnloginDeniey().getRealname();
        staticService.insStat(articleSearchVo.getPram());
        articleSearchVo.setUid(uid);

        articleSearchVo.setGid(gid);
        articleSearchVo.setRealName(uname);
        String post = HttpUtil.post(RESTURL_ES + "/search/search-nsfc", JSONObject.toJSONString(articleSearchVo));
        JSONObject jsonObject = JSONObject.parseObject(post);
        JSONObject data = jsonObject.getJSONObject("data");
        return new ResultBean<>(data);
    }

    @GetMapping(value = "search-detail")
    public ResultBean<Object> searchDetail(Integer id) {
        CheckUtil.check(StringUtil.isNotNull(id), "id is not null");
        PmUtAidInfoNsfcHosp pmUtAidInfoNsfcHosp = pmUtAidInfoNsfcHospMapper.selectById(id);
        parseEntityService.parseEntity(pmUtAidInfoNsfcHosp);
        staticService.insStat(pmUtAidInfoNsfcHosp.getTitle());
        return new ResultBean<>(pmUtAidInfoNsfcHosp);
    }


}
